package com.qc31.comlib.choose.car

import androidx.fragment.app.Fragment
import com.gyf.immersionbar.ktx.immersionBar
import com.qc31.baselib.base.BaseActivity
import com.qc31.baselib.custom.MRadioToolbar
import com.qc31.baselib.utils.MLog
import com.qc31.comlib.Keys
import com.qc31.comlib.adapter.IFragmentPAdapter
import com.qc31.comlib.databinding.ActivityChooseCarBinding
import com.qc31.comlib.databinding.IncludeToolbarRadioBinding
import com.qc31.comlib.databinding.IncludeViewpagerMatchBinding
import com.qc31.baselib.interfaces.SimplePageChangeListener
import com.qc31.comlib.R

/**
 *@Author Jasper
 *@Time   2020/7/22
 *@Desc   选择车辆
 */
class ChooseCarActivity : BaseActivity<ActivityChooseCarBinding>(ActivityChooseCarBinding::inflate),
    SimplePageChangeListener {
    private lateinit var mToolBarVB: IncludeToolbarRadioBinding
    private lateinit var mViewPagerVB: IncludeViewpagerMatchBinding
    private val fragments by lazy { mutableListOf<Fragment>() }

    private var isVideo = false


    override fun initView() {
        MLog.e("-------------------------------")
        mToolBarVB = IncludeToolbarRadioBinding.bind(binding.root)
        mViewPagerVB = IncludeViewpagerMatchBinding.bind(binding.root)
        immersionBar { titleBar(mToolBarVB.mCarRadioBar) }
        isVideo = intent.getBooleanExtra(Keys.INTENT_IS_VIDEO, false)
        initData()
    }

    override fun setListener() {
        mToolBarVB.mCarRadioBar.setTitle(
            R.string.desc_toolbar_car_choose,
            R.string.desc_toolbar_car_search,
            R.string.desc_toolbar_car_history
        )
        mToolBarVB.mCarRadioBar.setCallBack(object : MRadioToolbar.IToolbarCallback {
            override fun startCallback() {
                mViewPagerVB.mViewPager.currentItem = 0
            }

            override fun centerCallback() {
                mViewPagerVB.mViewPager.currentItem = 1
            }

            override fun endCallback() {
                mViewPagerVB.mViewPager.currentItem = 2
            }
        })
    }

    override fun onPageSelected(position: Int) {
        mToolBarVB.mCarRadioBar.setCheck(position)
    }

    private fun initData() {
        fragments.add(ChooseCarFragment(isVideo))
        fragments.add(SearchFragment(isVideo))
        fragments.add(HistoryFragment(isVideo))
        mViewPagerVB.mViewPager.adapter = IFragmentPAdapter(supportFragmentManager, fragments)
        mViewPagerVB.mViewPager.offscreenPageLimit = 3
        mViewPagerVB.mViewPager.addOnPageChangeListener(this)
    }


}